package com.wjh.anyview.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wjh.anyview.dao.ClazzCourseMapper;
import com.wjh.anyview.dao.ClazzMapper;
import com.wjh.anyview.dao.CourseMapper;
import com.wjh.anyview.dao.UserMapper;
import com.wjh.anyview.entity.bo.clazzServiceBo.StudentClazzBo;
import com.wjh.anyview.entity.dto.CommonResult;
import com.wjh.anyview.entity.po.Clazz;
import com.wjh.anyview.entity.po.ClazzCourse;
import com.wjh.anyview.entity.po.Course;
import com.wjh.anyview.entity.po.User;
import com.wjh.anyview.service.ClazzService;
import com.wjh.anyview.service.constant.StringConstant;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * @author wjh
 * @date 2021/10/9 21:23
 * @Package com.wjh.anyview.service.impl
 */
@Service
@Slf4j
public class ClazzServiceImpl implements ClazzService {
    @Resource
    private ClazzMapper clazzMapper;

    @Resource
    private UserMapper userMapper;

    @Resource
    private CourseMapper courseMapper;

    @Resource
    private ClazzCourseMapper clazzCourseMapper;

    @Override
    @Transactional
    public CommonResult<?> saveClazz(Clazz clazz) {
        //查重
        QueryWrapper<Clazz> wrapper = new QueryWrapper<>();
        wrapper.eq("clazz_name", clazz.getClazzName());
        wrapper.eq("grade", clazz.getGrade());
        wrapper.eq("institution_id", clazz.getInstitutionId());

        //重复添加，无效
        if (clazzMapper.selectOne(wrapper) != null) {
            return CommonResult.operateFailWithMessage("该班级已经存在本学术机构！");
        }

        //添加成功
        if (clazzMapper.insert(clazz) > 0) {
            return CommonResult.operateSuccess();
        }
        return CommonResult.operateFailWithMessage(StringConstant.SYSTEM_BUSY);
    }

    @Override
    @Transactional
    public CommonResult<?> updateStudentClazz(StudentClazzBo studentClazzBo) {
        //数据不合法
        if (userMapper.selectById(studentClazzBo.getStudentId()) == null || clazzMapper.selectById(studentClazzBo.getClazzId()) == null) {
            log.warn("数据不合法：" + studentClazzBo);
            return CommonResult.operateFailWithMessage(StringConstant.DATA_ILLEGAL);
        }
        User user = new User();
        user.setId(studentClazzBo.getStudentId());
        user.setClazzId(studentClazzBo.getClazzId());
        //修改user
        if (userMapper.updateById(user) > 0) {
            return CommonResult.operateSuccess();
        }
        return CommonResult.operateFailWithMessage(StringConstant.SYSTEM_BUSY);
    }

    @Override
    @Transactional
    public CommonResult<?> arrangeCourse(ClazzCourse clazzCourse) {
        //查重
        QueryWrapper<ClazzCourse> wrapper = new QueryWrapper<>();
        wrapper.eq("clazz_id", clazzCourse.getClazzId());
        wrapper.eq("course_id", clazzCourse.getCourseId());
        if (clazzCourseMapper.selectOne(wrapper) != null) {
            return CommonResult.operateFailWithMessage("该班级已经存在该课程！");
        }
        //查询指定班级
        Clazz clazz = clazzMapper.selectById(clazzCourse.getClazzId());
        //查询指定课程
        Course course = courseMapper.selectById(clazzCourse.getCourseId());
        //课程id或班级id不合法
        if (course == null || clazz == null) {
            log.info("数据不合法：" + clazzCourse);
            return CommonResult.operateFailWithMessage(StringConstant.DATA_ILLEGAL);
        }
        //课程不适合该班机
        if (!(course.getGrade().equals(clazz.getGrade()) && course.getInstitutionId().equals(clazz.getInstitutionId()))) {
            log.info("该课程不适合该班级，班级对象：" + clazz + "，课程对象：" + course);
            return CommonResult.operateFailWithMessage(StringConstant.DATA_ILLEGAL);
        }
        //插入数据
        if (clazzCourseMapper.insert(clazzCourse) > 0) {
            return CommonResult.operateSuccess();
        }
        return CommonResult.operateFailWithMessage(StringConstant.SYSTEM_BUSY);
    }

    @Override
    @Transactional
    public CommonResult<?> deleteClazzById(Long clazzId) {
        //通过id删除班级信息
        if (clazzMapper.deleteById(clazzId) > 0) {
            QueryWrapper<User> userWrapper = new QueryWrapper<>();
            userWrapper.eq("clazz_id", clazzId);
            userMapper.selectList(userWrapper).forEach(u -> {
                User user = new User();
                //根据id来更新
                user.setId(u.getId());
                //设置为没有班级
                user.setClazzId(0L);
                //更新对应的学生
                userMapper.updateById(user);
            });
            QueryWrapper<ClazzCourse> clazzCourseWrapper = new QueryWrapper<>();
            clazzCourseWrapper.eq("clazz_id", clazzId);
            //课程和班级的中间关系也要删除
            clazzCourseMapper.delete(clazzCourseWrapper);
            return CommonResult.operateSuccess();
        }
        return CommonResult.operateFailWithMessage(StringConstant.DATA_ILLEGAL);
    }

}
